<template>
  <!-- 导航栏 -->
  <van-nav-bar title="申请退款" left-arrow @click-left="back" />
  <section class="aui-flexView">
    <!-- 订单详细 -->
    <section class="aui-scrollView">
      <!-- 订单状态 -->
      <div class="aui-pay-change">
        <div class="aui-flex">
          <div class="aui-flex-box">
            <h3>{{ info.status_text }}</h3>
          </div>
          <div class="aui-pay-zfb">
            <img src="/assets/images/icon-zfb.png" alt="" />
          </div>
        </div>
      </div>

      <!-- 订单商品 -->
      <div class="aui-commodity-bg" style="background: transparent">
        <div
          style="margin-bottom: 10px; background: #f5f5f5"
          v-for="item in info.detail"
          :key="item.id"
        >
          <div class="aui-flex">
            <div class="aui-commodity-img">
              <img :src="item.product.thumb_text" />
            </div>
            <div class="aui-flex-box">
              <h4 class="van-ellipsis">商品名称：{{ item.product.name }}</h4>
              <p>商品单价：￥{{ item.price }}</p>
              <span class="aui-barter-time">七天退换</span>
            </div>
            <div>
              <h5>￥{{ item.total }}</h5>
              <p class="aui-sml-text">X{{ item.pronum }}</p>
            </div>
          </div>

          <div style="text-align: right">
            <van-button type="primary" size="mini" v-if="item.status == '3'"
              >评价
            </van-button>
          </div>
        </div>
      </div>

      <!-- 退款信息 -->
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="status_text"
            is-link
            readonly
            label="退款类型"
            placeholder="点击退款类型"
            @click="showPicker = true"
            :rules="[{ required: true, message: '请选择退款类型' }]"
          />
          <van-popup v-model:show="showPicker" position="bottom">
            <van-picker
              :columns="columns"
              @confirm="onConfirm"
              @cancel="showPicker = false"
            />
          </van-popup>
          <van-field
            v-model="refundreason"
            rows="2"
            autosize
            label="退款原因"
            type="textarea"
            maxlength="50"
            placeholder="请输入退款原因"
            show-word-limit
            :rules="[{ required: true, message: '请填写退款原因' }]"
          />
        </van-cell-group>
        <div style="margin: 16px">
          <van-button round block type="primary" native-type="submit"> 提交 </van-button>
        </div>
      </van-form>
    </section>
  </section>
</template>
<script setup>
const { proxy } = getCurrentInstance();
const back = () => proxy.$router.push("/order/index");
let business = proxy.$cookies.get("business") ? proxy.$cookies.get("business") : {};
business = reactive(business);
let orderid = proxy.$route.query.hasOwnProperty("orderid")
  ? proxy.$route.query.orderid
  : 0;
orderid = ref(orderid);
const info = ref({
  address: {},
  detail: [],
});
const status_text = ref("");
const showPicker = ref(false);
const columns = [
  { text: "仅退款", value: "-1" },
  { text: "退款退货", value: "-2" },
];
const refundreason = ref("");
const status = ref("");
const onConfirm = ({ selectedOptions }) => {
  status_text.value = selectedOptions[0]?.text;
  status.value = selectedOptions[0]?.value;
  showPicker.value = false;
};
onBeforeMount(() => {
  getInfoData();
});
const getInfoData = async () => {
  let result = await proxy.$POST({
    url: "/order/info",
    params: {
      orderid: orderid.value,
      busid: business.id,
    },
  });
  if (result && result.code == 1) {
    info.value = result.data;
  }
};

const onSubmit = async () => {
  let result = await proxy.$POST({
    url: "/order/refund",
    params: {
      orderid: orderid.value,
      busid: business.id,
      status: status.value,
      refundreason: refundreason.value,
    },
  });
  if (result.code == 0) {
    proxy.$fail(result.msg);
    return false;
  } else {
    proxy.$success({
      message: result.msg,
      duration: 1000,
      onClose: () => {
        proxy.$router.push('/order/index');
        return false;
      },
    });
  }
};
</script>
